<?php
/**
 * Name: 权限实现.
 *
 * Author:李强 EdwinLee
 * Company:上海煦鸣网络科技有限公司
 * Time: 2018-01-10 21:25
 */

namespace App\Services\Repo;


use App\Models\Permission;
use App\Services\Interfaces\RepoServiceInterface;
use App\Services\Service\DataService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PermissionRepo implements RepoServiceInterface
{

    /**
     * 查询数据集合
     * @param $request 条件
     */
    public function index(Request $request)
    {
        //获取节点列表
        $menus = Permission::submenus(0, false, true);
        return $menus;
    }

    /**
     * 新增数据
     * @param array|null $_options //数据参数
     * @return Model
     */
    public function store(array $_options = null): array
    {
        $result = ['code' => 0, 'message' => '保存失败'];
        $permission = new Permission();
        $permission->pid = isset($_options['pid']) ? $_options['pid'] : 0;
        $permission->display_name = isset($_options['display_name']) ? $_options['display_name'] : '';
        $permission->controller = strtolower(isset($_options['controller']) ? $_options['controller'] : '');
        $permission->actions = isset($_options['actions']) ? $_options['actions'] : '';
        $permission->name = strtolower($permission->controller . '.' . $permission->actions);
        //判断如果已经存在报错
        if (Permission::hasName($permission->name)) {
            $result['message'] = '权限已存在,不能重复添加';
        } else {
            $permission->description = isset($_options['description']) ? $_options['description'] : '';
            $permission->is_menu = isset($_options['is_menu']) ? 1 : 0;
            $permission->sort = isset($_options['sort']) ? $_options['sort'] : 0;
            $permission->icon = isset($_options['icon']) ? $_options['icon'] : '';

            if (false !== $permission->save()) {
                $result['code'] = 1;
                $result['message'] = '保存成功';
            }
        }
        return $result;
    }

    /**
     * 查询数据详情
     * @param array $_filter 过滤条件
     * @return Model
     */
    public function show(array $_filter): Model
    {
        $result = Permission::find($_filter)->first();
        if ($result) {
            return $result;
        }
    }

    /**
     * 修改数据
     * @param array $_filter 过滤条件
     * @param array $_update_data 要修改的数据
     * @return bool
     */
    public function update(array $_filter, array $_update_data): array
    {
        $result = ['code' => 0, 'message' => '保存失败'];
        $permission = Permission::where($_filter)->first();
        if (is_null($permission)) {
            $result['message'] = '查询权限菜单失败';
            return $result;
        }
        $pid = isset($_update_data['pid']) ? $_update_data['pid'] : 0;
        //判断父级不能是自己或自己的子级
        if (Permission::isInChild($_filter['id'], $pid)) {
            $result['message'] = '父级不能是自己或子级';
            return $result;
        }
        $permission->pid = $pid;
        $permission->display_name = isset($_update_data['display_name']) ? $_update_data['display_name'] : '';
        $permission->controller = strtolower(isset($_update_data['controller']) ? $_update_data['controller'] : '');
        $permission->actions = isset($_update_data['actions']) ? $_update_data['actions'] : '';
        $permission->name = strtolower($permission->controller . '.' . $permission->actions);
        //判断如果已经存在报错
        if (Permission::hasName($permission->name, $_filter['id'])) {
            $result['message'] = '权限已存在,不能重复添加';
        } else {
            $permission->description = isset($_update_data['description']) ? $_update_data['description'] : '';
            $permission->is_menu = isset($_update_data['is_menu']) ? 1 : 0;
            $permission->sort = isset($_update_data['sort']) ? $_update_data['sort'] : 0;
            $permission->icon = isset($_update_data['icon']) ? $_update_data['icon'] : '';

            if (false !== $permission->save()) {
                $result['code'] = 1;
                $result['message'] = '保存成功';
            }
        }
        return $result;
    }

    /**
     * 删除数据
     * @param array $_filter 过滤条件
     * @return bool
     */
    public function destroy(array $_filter): array
    {
        $result = ['code' => 0, 'message' => '保存失败'];
        $permisson = Permission::where($_filter)->first();
        if (is_null($permisson)) {
            $result['message'] = '查找功能权限失败';
            return $result;
        }
        //判断是否存在子级
        $children = Permission::where('pid', $_filter['id'])->get();
        if (count($children)) {
            $result['message'] = '存在子级功能权限,不能删除';
            return $result;
        }
        if (false !== $permisson->delete()) {
            //删除角色关联数据
            DB::table('permission_role')->where('permission_id', $_filter['id'])->delete();
            $result['message'] = '删除成功';
            $result['code'] = 1;
        }
        return $result;
    }
}